package com.redxiii.tracplus.ejb.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AppConfiguration {
	
	public static enum AppParam {
		TRAC_HOST_IP,
		LOGIN_DOMAIN_AUTH,
		CREDENTIAL_REQUIRED,
		DUMMY_SEARCH_MANAGER,
	}
	
	private static final Logger logger = LoggerFactory.getLogger(AppConfiguration.class);
	private static final AtomicInteger INSTANCE_ID = new AtomicInteger(1);
	
	private static AppConfiguration instance;
	private PropertiesConfiguration configuration;
	
	private AppConfiguration() {
		String url = System.getProperty("jboss.server.config.url") + "/tracplus.properties";
		logger.debug("Loading Configuration file ({}): '{}'", INSTANCE_ID.getAndIncrement(), url);
		try {
			configuration = new PropertiesConfiguration(url);
			configuration.setAutoSave(true);
			configuration.setReloadingStrategy(new FileChangedReloadingStrategy());	// 5 seconds
			
			logger.debug("Configuration file '{}' loaded", url);
		} catch (ConfigurationException e) {
			logger.error("Unable to load configuration file {}", e, url);
		} 
	}
	
	
	public synchronized static AppConfiguration getInstance() {
		if (instance == null)
			instance = new AppConfiguration();
		
		return instance;
	}
	
	public String getAsString(AppParam param) {
		return configuration.getString(param.name());
	}
	public Boolean getAsBoolean(AppParam param, Boolean defaultValue) {
		return configuration.getBoolean(param.name(), defaultValue);
	}
	
	public Long getQueryCount() {
		synchronized (configuration) {
			return configuration.getLong("stats.query-count.all", 0);
		}
	}
	
	public Long getQueryCountMonth() {
		synchronized (configuration) {
			return configuration.getLong("stats.query-count." + monthFormat.format(new Date()), 0);
		}
	}
	
	public Long getQueryCountUser(String userid) {
		synchronized (configuration) {
			return configuration.getLong("stats.query-count.user." + userid, 0);
		}
	}
	
	public Long getIncrementQueryCount(String userid) {
		synchronized (configuration) {
			long total = configuration.getLong("stats.query-count.all", 0) + 1L;
			configuration.setProperty("stats.query-count.all", total);
			
			long userCount = configuration.getLong("stats.query-count.user." + userid, 0) + 1L;
			configuration.setProperty("stats.query-count.user." + userid, userCount);
			
			String monthKey = "stats.query-count." + monthFormat.format(new Date());
			long monthCount = configuration.getLong(monthKey, 0) + 1L;
			configuration.setProperty(monthKey, monthCount);
			
			return total;
		}
	}
	
	private static final SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
	
}
